package com.qhyt.utils;


import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by dell on 2020/6/10.
 */
public class HttpUtils {

    public static final String LANG_NAME_COOKIE = "lang";


    public static String getLangFromCookie() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String lang = null;
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (null != cookie && LANG_NAME_COOKIE.equalsIgnoreCase(cookie.getName())) {
                    lang = cookie.getValue();
                    break;
                }
            }
        }
        if (lang == null) {
            lang = "zh-CN";
        }
        return lang;
    }

}
